home *** CD-ROM | disk | FTP | other *** search
/ Chip 1996 November / Chip 11-96.iso / workshop / howto / swap < prev    next >
Text File  |  1996-05-25  |  9KB  |  249 lines

  1. Newsgroups: comp.os.linux.announce
  2. From: hpa@eecs.nwu.edu (H. Peter Anvin N9ITP)
  3. Subject: Mini-HOWTO: Linux/MS-Windows swap space sharing [revised]
  4. Reply-To: hpa@nwu.edu (H. Peter Anvin)
  5. Organization: Northwestern University Electromagnetics Laboratory
  6. Approved: linux-announce@tc.cornell.edu (Matt Welsh)
  7.  
  8. Dear Linux friends,
  9. about two weeks ago I posted a mini-HOWTO about how to share swap
  10. spaces between Linux and MS-Windows.  However, I have since gotten
  11. feedback indicating some details were unique to my particular setup.
  12. Hence, here is the revised version, that hopefully will work better.
  13.  
  14. Note: the included scripts have changed.  I am not 100% sure, but I
  15. have a suspicion the problems stemmed from a different interpretation
  16. of back quotes `..` in bash 1.12 and 1.13.  The revised versions
  17. should work for all versions of sh.
  18.  
  19.        SHARING SWAP SPACES BETWEEN LINUX AND MS-WINDOWS
  20.  
  21.                  A mini-HOWTO
  22.                   by
  23.               Peter Anvin <hpa@nwu.edu>
  24.  
  25.           Copyright (C) 1994 H. Peter Anvin
  26.  
  27. VERSION: 1.1                        Date: 14 Apr 1994
  28.  
  29. 0. ABSTRACT
  30.  
  31. Many people use both Linux and MS-Windows.  The ability to do so is an
  32. important part of "the Linux revolution"; i.e. letting people
  33. experiment with (and get hooked on) Linux while still being able to
  34. run their off-the-shelf software.  Since both Linux and MS-Windows use
  35. virtual memory with swap to disk, a frequently occurring question in
  36. comp.os.linux.help is how to share swap spaces, in order to reduce the
  37. amount of disk space needed.
  38.  
  39. There are several methods for sharing swap spaces, the one described
  40. in this document is probably the most complicated one but is the only
  41. one I have encountered that allows maximum performance for both
  42. environments without the risk of trashing a disk partition.
  43.  
  44. 1. WHAT YOU NEED
  45.  
  46. This procedure have a few requirements that need to be filled.  I
  47. strongly recommend that you fill these requirements *anyway*, as there
  48. are several problems with older versions.
  49.  
  50.     * MS-DOS 5.0 or newer [tested with 6.2]
  51.     * MS-Windows 3.1 or newer
  52.     * A shutdown/init that knows to run a file on shutdown.
  53.       (The SysVinit-2.50 package can do this, for example.
  54.       SysVinit-2.50 is available from sunsite.unc.edu in
  55.       /pub/Linux/system/Daemons)
  56.  
  57. 2. THE PROCEDURE
  58.   
  59. * Boot DOS.  Create a DOS partition (using FDISK) the size = the size
  60.   swap space you want.  It will be assigned a drive letter; use that
  61.   drive letter instead of X whenever these instructions lists a
  62.   command like "LABEL X:" or "COPY FOO X:DUMMY.DAT"
  63.  
  64. * Format this partition using the DOS FORMAT command.
  65.     FORMAT X:
  66.  
  67. * Set the volume label on this partition to "SWAP SPACE" using the DOS
  68.   LABEL command.  Verify it by the DIR command.  Please do this as a
  69.   separate step.  Some versions of FORMAT do not seem to put the
  70.   volume label in the boot sector as it should.  [Note: some people
  71.   has written me saying the volume label is stored in the root
  72.   directory.  Yes, but at least since DOS 5.0 it has also been in the
  73.   boot sector.]
  74.     LABEL X:
  75.     DIR X:
  76.  
  77. * Start Windows.  Go to the Control Panel, select "386 Enhanced".
  78.   Select "Virtual Memory" and create a Windows Permanent swap file on
  79.   drive X: of maximum size (Windows will tell you the maximum size).
  80.   Windows may complain saying it will not use a swap file that big.
  81.   Ignore the message and create the file anyway.
  82.  
  83. * Exit Windows.
  84.  
  85. * Boot Linux, then log in as root.
  86.  
  87. * Use the fdisk command to find the name of the partition and its size
  88.   in blocks.  Create a symbolic link from /dev/winswap to this
  89.   partition.  If the paritition is hda7, then type:
  90.     ln -s /dev/hda7 /dev/winswap
  91.  
  92.   [NOTE TO PURISTS: Please use a symlink.  The name of this partition
  93.   is going to go into several configuration files and inconsistencies
  94.   could be fatal.]
  95.  
  96. * The following is a shell script that analyzes the partition and
  97.   derives some special information; extract it as "msinfo", set it
  98.   executable and run it as:
  99.     msinfo /dev/winswap
  100.  
  101. ----[CUT HERE]----
  102. #!/bin/sh
  103. #
  104. # Extract special sector information from an MS-DOS partition
  105. #
  106.  
  107. PATH=/bin:/usr/bin:/usr/local/bin
  108.  
  109. if [ "$#" != "1" ]; then
  110.   echo "Usage: $0 <partition-name>"
  111.   exit 1
  112. fi
  113.  
  114. if [ ! -r $1 ]; then
  115.   echo "$1: Permission denied"
  116.   exit 1
  117. fi
  118.  
  119. DOSVER="`dd 2>/dev/null if=$1 bs=1 count=8 skip=3`"
  120. SECSIZE=`dd 2>/dev/null if=$1 bs=1 count=2 skip=11 | hexdump -e '1/2 "%u\n"'`
  121. RESERV=`dd 2>/dev/null if=$1 bs=1 count=2 skip=14 | hexdump -e '1/2 "%u\n"'`
  122. FATS=`dd 2>/dev/null if=$1 bs=1 count=1 skip=16 | hexdump -e '1/1 "%u\n"'`
  123. ROOTDIR=`dd 2>/dev/null if=$1 bs=1 count=2 skip=17 | hexdump -e '1/2 "%u\n"'`
  124. FATSIZE=`dd 2>/dev/null if=$1 bs=1 count=2 skip=22 | hexdump -e '1/2 "%u\n"'`
  125. LABEL="`dd 2>/dev/null if=$1 bs=1 count=11 skip=43`"
  126. FILESYS="`dd 2>/dev/null if=$1 bs=1 count=8 skip=54`"
  127.  
  128. let FATSEC=$FATSIZE*$FATS
  129. let ENTPERSEC=$SECSIZE/32
  130. let ROOTSEC=$ROOTDIR/$ENTPERSEC
  131. let EXTRA=$ROOTDIR%$ENTPERSEC
  132. if [ $EXTRA != 0 ]; then let ROOTSEC=$ROOTSEC+1; fi
  133. let SPECIAL=$RESERV+$FATSEC+$ROOTSEC
  134.  
  135. echo "Formatting DOS version:      $DOSVER"
  136. echo "Filesystem:                  $FILESYS"
  137. echo "Volume label:                $LABEL"
  138. echo "Sector size:                 $SECSIZE"
  139. echo "Reserved sectors:            $RESERV"
  140. echo "FAT sectors:                 $FATSEC (${FATS}x${FATSIZE})"
  141. echo "Root directory sectors:      $ROOTSEC"
  142. echo "Total special sectors:       $SPECIAL"
  143. ----[END OF SCRIPT]----
  144.  
  145.   Take note at the number saying "Total special sectors", and verify
  146.   that the volume label says "SWAP SPACE".  If it does not, reboot DOS
  147.   and re-do the LABEL command.  If it still does not work, please
  148.   inform me about which version of DOS you are running, and I will try
  149.   to help you out.
  150.  
  151. * [Optional step] Windows may occationally leave some space on the
  152.   partition, even if it is told not to.  Don't attempt to use this
  153.   space, since it will be erased any time you run Linux.  If you want
  154.   to avoid accidentally using it (and lose data), you can create a
  155.   dummy file that fills that space by using the following commands:
  156.     mkdir /mnt
  157.     mount -t msdos /dev/winswap /mnt
  158.     dd if=/dev/zero of=/mnt/dummy.fil
  159.     umount /mnt  
  160.  
  161.   The dd command will report "No space left on device".  This is
  162.   exactly what you want.
  163.  
  164. * Check the name of the shutdown file.  For SysVinit this is the file
  165.   listed in the following line of /etc/inittab; add it if you don't
  166.   have it.
  167.  
  168.     # Runlevel 0 means shut down the system
  169.     l0:0:wait:/etc/brc
  170.  
  171.   For the remainder of this file, I will assume the filename was
  172.   /etc/brc.
  173.  
  174. * Type:
  175.  
  176.     dd if=/dev/winswap bs=512 count=XXX | gzip -9 > /etc/winswap.gz
  177.                                         ^^^
  178.                                         Replace
  179.   ... where XXX is replaced with the "Total special sectors" number.
  180.  
  181. * Add the following piece of code to your /etc/rc file (or whatever
  182.   your init calls it), right before the command "swapon -a" (if there
  183.   is no such command, add it to your /etc/rc file right before any
  184.   mount commands). 
  185.  
  186.   If your swapon is in /etc, replace /sbin/swapon with /etc/swapon.
  187.   If it is in /bin, replace with /bin/swapon.  Do the same for mkswap.
  188.  
  189.   Replace XXXXX with the actual size of the partition in blocks, as
  190.   given by fdisk.
  191.  
  192. ---[BEGIN CODE SEGMENT]---
  193. #
  194. # Verify and initialize swap space
  195. #
  196. echo -n 'Verifying swap space... '
  197. if [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=10 skip=4086`" \
  198.    = 'SWAP-SPACE' ]; then
  199.   echo 'Linux signature found'
  200.   /sbin/swapon /dev/winswap
  201. elif [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=11 skip=43`" \
  202.    = 'SWAP SPACE ' ]; then
  203.   echo 'DOS signature found'
  204.   /sbin/mkswap /dev/winswap XXXXX
  205.   /sbin/swapon /dev/winswap
  206. else
  207.   echo 'No signature found'
  208.   echo 'ERROR: Will not swap'
  209. fi
  210. ---[END CODE SEGMENT]---
  211.  
  212. * Add the following piece of code to your /etc/brc file (or whatever
  213.   it is called); put this after any command that might need swap to be
  214.   in place.
  215.  
  216. ---[BEGIN CODE SEGMENT]---
  217. #
  218. # Terminate swapping and restore DOS/Windows swap info
  219. #
  220. /sbin/swapoff /dev/winswap
  221. if [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=10 skip=4086`" \
  222.    = 'SWAP-SPACE' ]; then
  223.   echo 'Restoring DOS/Windows swap info'
  224.   /bin/zcat /etc/winswap.gz | /bin/dd of=/dev/winswap 2>/dev/null
  225. else
  226.   echo 'ERROR: /dev/winswap lacks swap signature, skipping restore'
  227. fi
  228. ---[END CODE SEGMENT]---
  229.  
  230. * Reboot Linux.  You should now have swapping on the new swap device.
  231.  
  232. 3. A COUPLE OF NOTES
  233.  
  234. * There is no need to add /dev/winswap to your /etc/fstab file.
  235.  
  236. * If your Linux session crashes or otherwise exits without running
  237.   /etc/brc, you will need to reboot and exit Linux before swapping in
  238.   Windows will work.  It is also possible to FORMAT X: and re-create
  239.   the Windows swapfile.  The only way around this would be to put the
  240.   equivalent of the /etc/brc commands in the DOS AUTOEXEC.BAT file;
  241.   unfortunately I don't know of any way of doing that in DOS!
  242.  
  243.  
  244. -- 
  245. INTERNET: hpa@nwu.edu               FINGER/TALK: hpa@ahab.eecs.nwu.edu
  246. IBM MAIL: I0050052 at IBMMAIL       HAM RADIO:   N9ITP or SM4TKN
  247. FIDONET:  1:115/511 or 1:115/512    STORMNET:    181:294/101
  248. #include <sig/virus.h>
  249.